/* ***** BEGIN LICENSE BLOCK *****
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Netscape Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/NPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is lutsko.com code.
 *
 * The Initial Developer of the Original Code is  James Lutsko 
 * (jim@lutsko.com http://www.lutsko.com).
 * Portions created by the Initial Developer are Copyright (C) 2003
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the NPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the NPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */
package autoschedule.ui;

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

import autoschedule.AutoScheduleDoc;
import autoschedule.Facility;
import autoschedule.League;
import autoschedule.ScheduleMsg;
import autoschedule.Team;
import autoteam.ui.AutoTeamTablePanel;


public class AutoScheduleFrame extends JPanel implements Observer {

    /** Each frame is the gui for one AutoScheduleDoc.*/
    AutoScheduleDoc doc = null;

    /** Display for the system log */
    JTextArea log = null;

    /** Used for editing schedules */
    JSplitPane splitPane = null;

    transient Facility theFacility = null;
    transient League theLeague = null;

    LeaguePanel leaguePanel = null;
    FacilityPanel facilityPanel = null;
    JPanel reportPanel = null;
    JPanel schedulePanel = null;
    JPanel editPanel = null;
    JPanel calendarPanel = null;

    FacilityStateModel facilityStateModel = null;

    final static String facility = "Facility";
    final static String league = "League";
    final static String schedule = "Schedule";
    final static String report = "Report";
    final static String edit = "Edit";
    final static String calendar = "Calendar";

    ScheduleTableModel scheduleTableModel = null;
    AutoTeamTablePanel scheduleTablePanel = null;

    public AutoScheduleFrame(AutoScheduleDoc _doc) {
	//	super((_doc == null ? "Scheduler" : _doc.getName()));
	super();

	//...Create the GUI and put it in the window...
	doc = _doc; 
	create();
	//...Then set pack...
	//	pack();
	//Set the window's location.
	//setLocation(xOffset*openFrameCount, yOffset*openFrameCount);
	
	if(doc != null) doc.addObserver(this);
    }

    void showTeam(Team t,  boolean isSchedule)
    {
	theFacility = null;
	if(!isSchedule) 
	    {
		theLeague = t.getLeague();
		showPanel(league);
	    } else {
		theLeague = null;
		scheduleTableModel.setTeamFilter(t); 
		showPanel(schedule); 
	    }
    }

    void showFacility(Facility f, boolean isSchedule)
    {
	theFacility = f;
	theLeague = null;
	if(!isSchedule) showPanel(facility);
	else {scheduleTableModel.setFacilityFilter(f); showPanel(schedule); }
    }

    void showLeague(League l, boolean isSchedule)
    {
	theLeague = l;
	theFacility = null;
	if(!isSchedule) showPanel(league);
	else {scheduleTableModel.setLeagueFilter(l); showPanel(schedule); }
    }

    void showSchedule()
    {
	theLeague = null;
	theFacility = null;
	showPanel(schedule);
    }

    void showReport()
    {
	theLeague = null;
	theFacility = null;
	showPanel(report);
    }
    
     void showCalendar()
    {
        calendarPanel = new GCalendar();
        theLeague = null;
        theFacility = null;
        showPanel(calendar);
    }
    void showPanel(String s)
    {
	leaguePanel.setLeague(theLeague);
	facilityPanel.setFacility(theFacility);
	CardLayout cl = (CardLayout)(this.getLayout());
	cl.show(this,s);
    }
   
    public void update(Observable o, Object _msg)
    {
	ScheduleMsg msg = (ScheduleMsg) _msg;
	
	if(msg.getMsg() == doc.SCHEDULE_CHANGED)
	    {
		scheduleTableModel.fireTableDataChanged();
		facilityStateModel.fireTableDataChanged();
	    } else if (msg.getMsg() == doc.SCHEDULE_SWITCH_ERROR) {
		JOptionPane.showMessageDialog(null,
					      "You cannot switch games with different time or space requirements or unscheduled games",
					      "Cannot do this",
					      JOptionPane.INFORMATION_MESSAGE,null);
	    }
    }
    void create()
    {
	// create the log
	if(doc != null)
	    log = new JTextArea(doc.getLogDocument(),null,5,20);
	else log = new JTextArea();
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(true);

	leaguePanel = new LeaguePanel(doc);
	facilityPanel =  new FacilityPanel(doc);	
	schedulePanel = createSchedulePanel();
	editPanel = createEditPanel();
	reportPanel = createReportPanel();
        calendarPanel = createCalendarPanel();
	setLayout(new CardLayout());

        this.add(calendarPanel, calendar);
	this.add(leaguePanel,league);
	this.add(facilityPanel,facility);

	splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
	splitPane.setTopComponent(schedulePanel);
	splitPane.setOneTouchExpandable(true); 

	this.add(splitPane,schedule);

	this.add(reportPanel,report);

       
	//titlePanel.setBackground(Color.white);
	this.setBackground(Color.white);

    }

    void setDoc(AutoScheduleDoc _doc)
    {
	doc = _doc;
	if(doc != null) doc.addObserver(this);

	leaguePanel.setDoc(doc);
	facilityPanel.setDoc(doc);
	scheduleTableModel.setDoc(doc);

	facilityStateModel.setDoc(doc);

	log = new JTextArea(doc.getLogDocument(),null,5,20);
        log.setMargin(new Insets(5,5,5,5));
        log.setEditable(true);

	
	this.remove(reportPanel);
	reportPanel = createReportPanel();
	this.add(reportPanel,report);
    }
    JPanel createCalendarPanel()
    {
        calendarPanel = new GCalendar();
        return calendarPanel;
    }

    JPanel createSchedulePanel()
    {
	JComboBox comboBox = new JComboBox();
	scheduleTableModel = new ScheduleTableModel(doc, comboBox);
	scheduleTablePanel = new AutoTeamTablePanel(scheduleTableModel);
	scheduleTablePanel.setTableCellEditor(0,comboBox);
	scheduleTablePanel.setTableCellEditor(1,comboBox);


	scheduleTablePanel.setTableDateCellEditor();	

	scheduleTablePanel.enableDrag();

	JCheckBox cb = new JCheckBox("Show availability");
	cb.addItemListener(new ItemListener() {
		public void itemStateChanged(ItemEvent e) {
		    System.out.println("State is " + (e.getStateChange() == ItemEvent.SELECTED));
		    if(e.getStateChange() == ItemEvent.SELECTED)
			{
			    splitPane.setBottomComponent(editPanel);
			} else splitPane.setBottomComponent(null);
		}});		    
	scheduleTablePanel.addComponent(cb);
	return scheduleTablePanel;

    }

    JPanel createEditPanel()
    {
	facilityStateModel = new FacilityStateModel(doc);
	final AutoTeamTablePanel theTablePanel = new AutoTeamTablePanel(facilityStateModel);	

	theTablePanel.enableDrag();

	final JComboBox cb = new JComboBox(new DefaultComboBoxModel() {
		public int getSize(){ return (doc == null ? 0 : doc.getNumberOfFacilities());}
		public Object getElementAt(int index){return doc.getFacility(index);}
		public int getIndexOf(Object anObject) {return doc.getIndexOfFacility(anObject);}
	    });

	cb.setPrototypeDisplayValue(new String("Facility Name"));

        cb.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    theTablePanel.stopEditing();
		    facilityStateModel.facility = doc.getFacility(cb.getSelectedIndex());
		    facilityStateModel.fireTableDataChanged();
		}});

	theTablePanel.addComponent(cb);


	return theTablePanel;
    }

    JPanel createReportPanel()
    {
	JScrollPane logScrollPane = new JScrollPane(log);
	JPanel pane = new JPanel();
        pane.setLayout(new BorderLayout());
	pane.add(logScrollPane, BorderLayout.CENTER);
	return pane;
    }

    // DND helper
}

